home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / cm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-25  |  9.5 KB  |  417 lines

  1. /* Cursor motion subroutines for XEmacs.
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.     loosely based primarily on public domain code written by Chris Torek
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: FSF 19.28.  Substantially different from FSF. */
  22.  
  23. #include <config.h>
  24. #include "lisp.h"
  25.  
  26. #include "device.h"
  27. #include "device-tty.h"
  28. #include "frame.h"
  29. #include "redisplay.h"
  30.  
  31. #define    EXPENSIVE 2000
  32.  
  33. extern char *tgoto (CONST char *cm, int hpos, int vpos);
  34. extern void tputs (CONST char *, int, void (*)(int));
  35.  
  36. static int cm_cost_counter;        /* sums up costs */
  37.  
  38. void evalcost (int c);
  39. void
  40. evalcost (int c)
  41. {
  42.   cm_cost_counter++;
  43. }
  44.  
  45. /* Ugh -- cmputc() can't take a device argument, so we pass it in a global */
  46. struct device *cmputc_device;
  47.  
  48. void
  49. cmputc (int c)
  50. {
  51.   if (termscript)
  52.     fputc (c, termscript);
  53.  
  54.   fputc (c, DEVICE_TTY_DATA (cmputc_device)->outfd);
  55. }
  56.  
  57. /*
  58.  * (Re)Initialize the cost factors, given the output speed of the
  59.  * terminal in DEVICE_TTY_DATA (d)->ospeed.  (Note: this holds B300,
  60.  * B9600, etc -- ie stuff out of <sgtty.h>.)
  61.  */
  62. void
  63. cm_cost_init (struct device *d)
  64. {
  65.   char *tmp;
  66.  
  67.   cm_cost_counter = 0;
  68. #define    COST(x,e) (x \
  69.            ? (cm_cost_counter = 0, tputs (x, 1, e), cm_cost_counter) \
  70.            : EXPENSIVE)
  71. #define MINCOST(x,e) ((x == 0) \
  72.               ? EXPENSIVE \
  73.               : (tmp = tgoto(x, 0, 0), COST(tmp,e)))
  74.  
  75.   TTY_COST (d).cm_up = COST (TTY_CM (d).up, evalcost);
  76.   TTY_COST (d).cm_down = COST (TTY_CM (d).down, evalcost);
  77.   TTY_COST (d).cm_left = COST (TTY_CM (d).left, evalcost);
  78.   TTY_COST (d).cm_right = COST (TTY_CM (d).right, evalcost);
  79.   TTY_COST (d).cm_home = COST (TTY_CM (d).home, evalcost);
  80.   TTY_COST (d).cm_low_left = COST (TTY_CM (d).low_left, evalcost);
  81.   TTY_COST (d).cm_car_return = COST (TTY_CM (d).car_return, evalcost);
  82.  
  83.   /*
  84.    * These last three are actually minimum costs.  When (if) they are
  85.    * candidates for the least-cost motion, the real cost is computed.
  86.    * (Note that "0" is the assumed to generate the minimum cost.
  87.    * While this is not necessarily true, I have yet to see a terminal
  88.    * for which is not; all the terminals that have variable-cost
  89.    * cursor motion seem to take straight numeric values.  --ACT)
  90.    */
  91.  
  92.   TTY_COST (d).cm_abs = MINCOST (TTY_CM (d).abs, evalcost);
  93.   TTY_COST (d).cm_hor_abs = MINCOST (TTY_CM (d).hor_abs, evalcost);
  94.   TTY_COST (d).cm_ver_abs = MINCOST (TTY_CM (d).ver_abs, evalcost);
  95.  
  96. #undef MINCOST
  97. #undef COST
  98. }
  99.  
  100. /*
  101.  * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
  102.  * up and down, and left and right, and motions.  If doit is set
  103.  * actually perform the motion.
  104.  */
  105.  
  106. #ifdef NOT_YET
  107. static int
  108. calccost (struct frame *f, int srcy, int srcx, int dsty, int dstx, int doit)
  109. {
  110.   struct device *d = XDEVICE (FRAME_DEVICE (f));
  111.   int totalcost = 0;
  112.   int deltay, deltax;
  113.   char *motion;
  114.   int motion_cost;
  115.  
  116. #if 0
  117.   int ntabs, n2tabs, tabx, tab2x, tabcost;
  118. #endif
  119.  
  120.   cmputc_device = d;
  121. #if 0
  122.     /* If have just wrapped on a terminal with xn,
  123.        don't believe the cursor position: give up here
  124.        and force use of absolute positioning.  */
  125.     if (curX == Wcm.cm_cols)
  126.       goto fail;
  127. #endif
  128.  
  129.   deltay = dsty - srcy;
  130.   if (!deltay)
  131.     goto calculate_x;
  132.  
  133.   if (deltay < 0)
  134.     {
  135.       motion = TTY_CM (d).up;
  136.       motion_cost = TTY_COST (d).cm_up;
  137.       deltay = -deltay;
  138.     }
  139.   else
  140.     {
  141.       motion = TTY_CM (d).down;
  142.       motion_cost = TTY_COST (d).cm_down;
  143.     }
  144.  
  145.   if (motion_cost == EXPENSIVE)
  146.     {
  147. /*      if (doit) */
  148.     /* #### printing OOF is not acceptable */
  149.       return motion_cost;
  150.     }
  151.  
  152.   totalcost = motion_cost * deltay;
  153.  
  154.   if (doit)
  155.     while (--deltay >= 0)
  156.       tputs (motion, 1, cmputc);
  157.  
  158. calculate_x: 
  159.  
  160.   deltax = dstx - srcx;
  161.   if (!deltax)
  162.     goto done;
  163.  
  164.   if (deltax < 0)
  165.     {
  166.       motion = TTY_CM (d).left;
  167.       motion_cost = TTY_COST (d).cm_left;
  168.       deltax = -deltax;
  169.     }
  170.   else
  171.     {
  172.       motion = TTY_CM (d).right;
  173.       motion_cost = TTY_COST (d).cm_right;
  174.     }
  175.  
  176.   if (motion_cost == EXPENSIVE)
  177.     {
  178. /*    if (doit) */
  179.         /* #### printing OOF is not acceptable */
  180.     return motion_cost;
  181.     }
  182.  
  183.   totalcost += motion_cost * deltax;
  184.  
  185.   if (doit)
  186.     while (--deltax >= 0)
  187.       tputs (motion, 1, cmputc);
  188.  
  189. done: 
  190.     return totalcost;
  191. }
  192. #endif /* NOT_YET */
  193.  
  194. #define    USEREL    0
  195. #define    USEHOME    1
  196. #define    USELL    2
  197. #define    USECR    3
  198.  
  199. #if OLD_CURSOR_MOTION_SHIT
  200. void
  201. cmgoto (struct frame *f, int row, int col)
  202. {
  203.   struct device *d = XDEVICE (FRAME_DEVICE (f));
  204.   char *motion;
  205. #if 0
  206.   int frame_x = FRAME_CURSOR_X(f);
  207.   int frame_y = FRAME_CURSOR_Y(f);
  208.   int relcost, directcost, llcost;
  209.   int homecost;
  210.   int use;
  211.   char *dcm;
  212. #endif
  213.  
  214.   cmputc_device = d;
  215.  
  216.   /* First the degenerate case */
  217. #if 0
  218.   if (row == frame_y && col == frame_x)
  219.     return;
  220. #endif
  221.  
  222.   /* #### something is fucked with the non-absolute cases */
  223.   motion = tgoto (TTY_CM (d).abs, col, row);
  224.   tputs (motion, 1, cmputc);
  225.   DEVICE_TTY_DATA (d)->cursor_x = col;
  226.   DEVICE_TTY_DATA (d)->cursor_y = row;
  227.   return;
  228.  
  229. #if 0
  230.   if (frame_y >= 0 && frame_x >= 0)
  231.     {
  232.       /* 
  233.        * Pick least-cost motions
  234.        */
  235.  
  236.       relcost = calccost (f, frame_y, frame_x, row, col, 0);
  237.       use = USEREL;
  238.  
  239.       homecost = TTY_COST (d).cm_home;
  240.       if (homecost < EXPENSIVE)
  241.     homecost += calccost (f, 0, 0, row, col, 0);
  242.  
  243.       if (homecost < relcost)
  244.     {
  245.       relcost = homecost;
  246.       use = USEHOME;
  247.     }
  248.  
  249.       llcost = TTY_COST (d).cm_low_left;
  250.       if (llcost < EXPENSIVE)
  251.     llcost += calccost (f, frame_y - 1, 0, row, col, 0);
  252.  
  253.       if (llcost < relcost)
  254.     {
  255.       relcost = llcost;
  256.       use = USELL;
  257.     }
  258.  
  259. #if 0
  260.       if ((crcost = Wcm.cc_cr) < BIG) {
  261.       if (Wcm.cm_autolf)
  262.           if (curY + 1 >= Wcm.cm_rows)
  263.           crcost = BIG;
  264.           else
  265.           crcost += calccost (curY + 1, 0, row, col, 0);
  266.       else
  267.           crcost += calccost (curY, 0, row, col, 0);
  268.       }
  269.       if (crcost < relcost)
  270.       relcost = crcost, use = USECR;
  271. #endif
  272.  
  273.       directcost = TTY_COST (d).cm_abs;
  274.       dcm = TTY_CM (d).abs;
  275.  
  276.       if (row == frame_y && TTY_COST (d).cm_hor_abs < EXPENSIVE)
  277.     {
  278.       directcost = TTY_COST (d).cm_hor_abs;
  279.       dcm = TTY_CM (d).hor_abs;
  280.     }
  281.       else if (col == frame_x && TTY_COST (d).cm_ver_abs < EXPENSIVE)
  282.     {
  283.       directcost = TTY_COST (d).cm_ver_abs;
  284.       dcm = TTY_CM (d).ver_abs;
  285.     }
  286.     }
  287.   else
  288.     {
  289.       directcost = 0;
  290.       relcost = 100000;
  291.       dcm = TTY_CM (d).abs;
  292.     }
  293.  
  294.   /* 
  295.    * In the following comparison, the = in <= is because when the costs
  296.    * are the same, it looks nicer (I think) to move directly there.
  297.    */
  298.   if (directcost <= relcost)
  299.     {
  300.       /* compute REAL direct cost */
  301.       cm_cost_counter = 0;
  302.       motion = (dcm == TTY_CM (d).hor_abs
  303.         ? tgoto (dcm, row, col)
  304.         : tgoto (dcm, col, row));
  305.       tputs (motion, 1, evalcost);
  306.       if (cm_cost_counter <= relcost)
  307.     {    /* really is cheaper */
  308.       tputs (motion, 1, cmputc);
  309.       FRAME_CURSOR_Y (f) = row;
  310.       FRAME_CURSOR_X (f) = col;
  311.       return;
  312.     }
  313.     }
  314.  
  315.   switch (use)
  316.     {
  317.     case USEHOME: 
  318.       tputs (TTY_CM (d).home, 1, cmputc);
  319.       FRAME_CURSOR_X (f) = 0;
  320.       FRAME_CURSOR_Y (f) = 0;
  321.       break;
  322.  
  323.     case USELL: 
  324.       tputs (TTY_CM (d).low_left, 1, cmputc);
  325.       FRAME_CURSOR_Y (f) = FRAME_HEIGHT (f) - 1;
  326.       FRAME_CURSOR_X (f) = 0;
  327.       break;
  328.  
  329. #if 0
  330.     case USECR: 
  331.       tputs (Wcm.cm_cr, 1, cmputc);
  332.       if (Wcm.cm_autolf)
  333.     curY++;
  334.       curX = 0;
  335.       break;
  336. #endif
  337.     }
  338.  
  339.   (void) calccost (f, FRAME_CURSOR_Y (f), FRAME_CURSOR_X (f), row, col, 1);
  340.   FRAME_CURSOR_Y (f) = row;
  341.   FRAME_CURSOR_X (f) = col;
  342. #endif
  343. }
  344. #endif /* OLD_CURSOR_MOTION_SHIT */
  345.  
  346. /*****************************************************************************
  347.  cmgoto
  348.  
  349.  This function is responsible for getting the cursor from its current
  350.  location to the passed location in the most efficient manner
  351.  possible.
  352.  ****************************************************************************/
  353. void
  354. cmgoto (struct frame *f, int row, int col)
  355. {
  356.   struct device *d = XDEVICE (f->device);
  357.   char *motion;
  358.  
  359.   cmputc_device = d;
  360.  
  361.   /* First make sure that we actually have to do any work at all. */
  362.   if (DEVICE_TTY_CURSOR_X (d) == col && DEVICE_TTY_CURSOR_Y (d) == row)
  363.     return;
  364.  
  365.   /* #### Need to reimplement cost analysis and potential relative
  366.      movement. */
  367.  
  368.   /* If all else fails, use absolute movement. */
  369.   motion = tgoto (TTY_CM (d).abs, col, row);
  370.   tputs (motion, 1, cmputc);
  371.   DEVICE_TTY_CURSOR_X (d) = col;
  372.   DEVICE_TTY_CURSOR_Y (d) = row;
  373. }
  374.  
  375. #if 0
  376. /* Clear out all terminal info.
  377.    Used before copying into it the info on the actual terminal.
  378.  */
  379.  
  380. void
  381. Wcm_clear (void)
  382. {
  383.   memset (&Wcm, 0, sizeof Wcm);
  384.   UP = 0;
  385.   BC = 0;
  386. }
  387. #endif
  388.  
  389. #if 0
  390. /*
  391.  * Initialized stuff
  392.  * Return 0 if can do CM.
  393.  * Return -1 if cannot.
  394.  * Return -2 if size not specified.
  395.  */
  396.  
  397. int
  398. Wcm_init (void)
  399. {
  400. #if 0
  401.   if (Wcm.cm_abs && !Wcm.cm_ds)
  402.     return 0;
  403. #endif
  404.   if (Wcm.cm_abs)
  405.     return 0;
  406.   /* Require up and left, and, if no absolute, down and right */
  407.   if (!Wcm.cm_up || !Wcm.cm_left)
  408.     return - 1;
  409.   if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right))
  410.     return - 1;
  411.   /* Check that we know the size of the frame.... */
  412.   if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0)
  413.     return - 2;
  414.   return 0;
  415. }
  416. #endif
  417.